/*
 * menu.c
 *
 *  Created on: 2021年11月1日
 *      Author: 王慧云
 */

#include "menu.h"
#include <string.h>

static lv_obj_t* list_menu;

lv_obj_t* menu_scr;
/*创建一个键盘事件组*/
lv_group_t* menu_keyGroup;


/**返回主界面 回调函数**/
static void exit_screen(void * scr)
{
  lv_obj_del(scr);
}

/*打开调节方式设置界面 的回调函数*/

/*打开PID设置回调函数  的回调函数*/


/*打开IO配置界面  的回调函数*/

/*打开导入配置界面 的回调函数*/

/*打开启动设置界面 的回调函数*/




static void list_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* obj = lv_event_get_target(e);

    if (code == LV_EVENT_PRESSED) {
        //LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list_menu, obj));
        //printf("Clicked: %s", lv_list_get_btn_text(list_menu, obj));
        if(strcmp(lv_list_get_btn_text(list_menu, obj),"关闭") == 0)
        {
        	lv_group_del(menu_keyGroup); //删除键盘组
        	lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
        	MotorCtrol_Home(); //打开主界面
        }
        else if(strcmp(lv_list_get_btn_text(list_menu, obj),"频率") == 0)
        {
        	if(motor_get_runStatus())
        	{
        		//如果电机处在运行状态不允许设置频率
        	}
        	else
        	{
        		lv_group_del(menu_keyGroup); //删除键盘组
        		lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
        		open_freqSetWin();//打开频率设置界面回调函数
        	}
        }
        else if(strcmp(lv_list_get_btn_text(list_menu,obj),"最大转速") == 0)
        {
            lv_group_del(menu_keyGroup);
            lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
            open_maxSpeedSetWin();//打开设置最大转速回调函数
        }
        else if(strcmp(lv_list_get_btn_text(list_menu,obj),"调节方式") == 0)
        {
            lv_group_del(menu_keyGroup);
            lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
            open_modeSetWin();
        }
        else if(strcmp(lv_list_get_btn_text(list_menu,obj),"PID设置") == 0)
        {
            lv_group_del(menu_keyGroup);
            lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
            open_pidSetWin();            
        }
        else if(strcmp(lv_list_get_btn_text(list_menu,obj),"IO配置") == 0)
        {
            lv_group_del(menu_keyGroup);
            lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
            open_IOsetWin();  
        }
        else if(strcmp(lv_list_get_btn_text(list_menu,obj),"Encoder") == 0)
        {
            lv_group_del(menu_keyGroup);
            lv_async_call(exit_screen, lv_scr_act()); //异步删除当前界面
            open_encoderSetWin();             
        }
        else if(strcmp(lv_list_get_btn_text(list_menu,obj),"背光调节") == 0)
        {
            lv_group_del(menu_keyGroup);
            lv_async_call(exit_screen,lv_scr_act());
            open_backlightSetWin();//打开背光调节设置
        }
    }
    //printf("\r\n");
}

void menu(uint8_t index)
{
    /*创建一个首页屏幕*/
    menu_scr = lv_obj_create(NULL);
    lv_scr_load(menu_scr);
    list_menu = lv_list_create(lv_scr_act());
    lv_obj_set_size(list_menu, 125, 80);
    lv_obj_align(list_menu, LV_ALIGN_TOP_RIGHT,0,0);

	menu_keyGroup= lv_group_create();
	lv_indev_set_group(indev_keypad, menu_keyGroup);

    /*创建一个按钮添加到list中*/
    lv_obj_t* btn[8];
    const char* name[] = {"频率","最大转速","调节方式",
                         "PID设置","IO配置","Encoder","背光调节", "关闭"};
    for(uint8_t i = 0; i < 8; i ++)
    {
        btn[i] = lv_list_add_btn(list_menu,NULL,name[i]);
        lv_obj_add_event_cb(btn[i], list_event_handler, LV_EVENT_PRESSED, NULL);
        lv_obj_set_style_text_font(btn[i],&yahei_16,0);
        lv_obj_set_height(btn[i],22);
        lv_obj_set_style_pad_bottom(btn[i],0,LV_PART_MAIN);
        lv_obj_set_style_pad_top(btn[i],-2,LV_PART_MAIN);        
    }
    for(uint8_t i = 0;i < 8; i ++)
    {
        lv_group_add_obj(menu_keyGroup, btn[index]);
        index++;
        if(index >= 8)
            index = 0;
    }
    

    lv_obj_t* widgets;
    widgets = lv_label_create(lv_scr_act());
    lv_label_set_long_mode(widgets, LV_LABEL_LONG_WRAP);     /*Break the long lines*/
    lv_obj_set_width(widgets, 32);  /*Set smaller width to make the lines wrap*/
    lv_obj_set_style_text_align(widgets, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(widgets, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_label_set_text(widgets,"运行状态");
    lv_obj_set_style_text_font(widgets, &yahei_16, 0);

    widgets = lv_led_create(lv_scr_act());
    lv_obj_align(widgets, LV_ALIGN_BOTTOM_LEFT, 5, -7);
    lv_obj_set_size(widgets,20,20);
    lv_led_set_color(widgets, lv_palette_main(LV_PALETTE_GREEN));
    if(motor_get_runStatus())
    {
    	lv_led_on(widgets);
    }
    else
    {
    	lv_led_off(widgets);
    }
}

